package com.xy.controller.uc;


import com.xy.common.base.ApiBaseResponse;

import com.xy.entity.uc.Area;
import com.xy.repository.AreaRepository;
import com.xy.repository.VersionRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Api(value = "省市区Controller", description = "省市区")
@RestController
@RequestMapping(value = "api/area")
public class AreaController {

    @Autowired
    private AreaRepository areaRepository;

    @Autowired
    private VersionRepository versionRepository;

    @ApiOperation("返回省市区")
    @ResponseStatus(HttpStatus.OK)
    @GetMapping(value = "/getArea")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="versionType",value = "版本类型",required=true,example = "省市区版本"),
            @ApiImplicitParam(name ="versionId",value = "版本id",required=true)
    })
    @Transactional
    public ResponseEntity getArea(@RequestParam("versionType") String versionType, @RequestParam("versionId") String versionId) {


        String serversionId = versionRepository.findAllByVersionType(versionType).getVersionId();
        if (serversionId.equals(versionId) == false) {
            //省市区所有信息
            List<Area> allAreas = areaRepository.findAll();
            List<Area> allProvince = new ArrayList<>();
            List<Area> allcities = new ArrayList<>();
            List<Area> allParts = new ArrayList<>();

            for (Area temp : allAreas) {

                if (temp.getAreaLevel().equals("1")) {

                    allProvince.add(temp);

                } else if (temp.getAreaLevel().equals("2")) {

                    allcities.add(temp);

                } else if (temp.getAreaLevel().equals("3")) {

                    allParts.add(temp);
                }
            }

            for (Area province : allProvince) {
                province.setSubArea(new ArrayList<>());
                for (Area city : allcities) {
                    city.setSubArea(new ArrayList<>());
                    if (city.getParentId().equals(province.getAreaId())) {
                        province.getSubArea().add(city);
                    }
                    for (Area part : allParts) {
                        if (part.getParentId().equals(city.getAreaId())) {
                            city.getSubArea().add(part);
                        }
                    }
                }
            }

            return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK, allProvince,serversionId));
        }
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK));
    }

}



